package com.txh.blog.service.impl;

import com.txh.blog.mapper.ArticleMapper;
import com.txh.blog.mapper.TypeMapper;
import com.txh.blog.service.AdminService;
import com.txh.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TypeMapper typeMapper;

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public Map<String, Object> getIndexData() {
        //获取博客数据
        Map<String,Object> data = articleMapper.articleData();
        relpaceValue(data);
        //总分类数
        Integer typeCount = typeMapper.typeCount();
        data.put("typeCount",typeCount);
        return data;
    }

    private void relpaceValue(Map<String,Object> data){
        if (data.size()!=0){
            if (data.get("readNumCount") == null){
                data.replace("readNumCount",0);
            }
            if (data.get("singleCount") == null){
                data.replace("singleCount",0);
            }
        }

    }
}
